package cn.yl.consumer.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    private final RestTemplate restTemplate;
    private final DiscoveryClient discoveryClient;

    public ConsumerController(DiscoveryClient discoveryClient, RestTemplate restTemplate) {
        this.discoveryClient = discoveryClient;
        this.restTemplate = restTemplate;
    }

    //    @Value("${server.port}")
//    private String port;
    @GetMapping("/port")
    public String getPort() {
//        ServiceInstance consumer = discoveryClient.getInstances("PROVIDER").get(0);
//        System.out.println("provider = " + consumer.getUri());
//        String url= consumer.getUri() + "/provider/port/";
        return restTemplate.getForObject("http://provider/provider/port",String.class);
    }
}
